package simulation;
import java.util.Vector;

import sim.engine.Schedule;
import sim.engine.SimState;
import sim.engine.Steppable;
import sim.util.Bag;
import view.Window;
import agents.Artiste;
import agents.FAI;
import agents.Gestionnaire;
import agents.Internaute;
import ec.util.MersenneTwisterFast;

public class Simulation extends SimState {
	
	
	public static Gestionnaire gestionnaire;
	public static FAI fai;
	public static Bag artistes = new Bag();
	public static Vector<String> artistesid = new Vector<String>();
	public static Styles styles;
	public static Bag oeuvres = new Bag();
	private static Simulation instance;
	public static Window window;
	public static Constantes constantes;
	
	public Simulation(long seed) {
		super(new MersenneTwisterFast(seed),new Schedule());
		// TODO Auto-generated constructor stub
	}

	public void start(){
		super.start();
		addView();
		addStyles();
		addAgentGestionnaire();
		addAgentFAI();
		addAgentArtiste();
		addAgentInternaut();
		instance = this;
	}

	public static void main(String[] args){
		doLoop(Simulation.class, args);
		System.exit(0);
	}
	
	public void addView () {
		Steppable agent = null;
		agent = new Window();
		window = (Window) agent;
		schedule.scheduleRepeating(agent);
	}
	
	
	public void addStyles(){
		Steppable agent =null; 
		agent = new Styles();
		styles = (Styles) agent;
		schedule.scheduleRepeating(agent);
	}
	
	public void addAgentInternaut(){
		for ( int i = 0; i < simulation.Constantes.nbInternaute; i ++ ){
			Steppable agent =null; 
			float a = (float)Math.random();
			float b = (float)Math.random();
			float c = (float)Math.random();
			float d = (float)Math.random();
			agent = new Internaute(a,b,c,d);
			for (int j = 0; j < simulation.Constantes.nbStylePreferesInternaute; j ++ ){
				((Internaute)agent).setStyle(getStyle());	
			}
			schedule.scheduleRepeating(agent);
		}
	}
	
	public void addAgentArtiste(){
		for ( int i = 0; i < Constantes.nbArtiste; i ++ ){
			System.out.println("ploup");
			Steppable agent =null; 
			String id = "Artiste " + i;
			float temp = (float)Math.random();
			boolean gest;
			if(temp > 0.5){
				gest = true;
			} else{
				gest = false;
			}
			float temp2 = (float)Math.random();
			float tmp = (float)Math.random()*5;
			agent = new Artiste(id, gest, 20, temp2, getStyle(), tmp);
			Simulation.artistes.add(agent);
			this.artistesid.add(id);
			schedule.scheduleRepeating(agent);
		}
		
	}
	
	public void addAgentGestionnaire(){
		Steppable agent =null; 
		agent = new Gestionnaire();
		this.gestionnaire = (Gestionnaire) agent;
		schedule.scheduleRepeating(agent);
	}
	
	public void addAgentFAI(){
		Steppable agent =null; 
		agent = new FAI();
		this.fai = (FAI) agent;
		schedule.scheduleRepeating(agent);
	}
	
	public String getStyle(){
		int i = styles.styles.numObjs;
		int rdm = (int)(Math.random()*i);
		Bag temp = (Bag)(styles.styles.get(rdm));
		i = temp.size();
		rdm = (int)(Math.random()*i);
		String res = (String) temp.get(rdm);
		return res;
	}
	

	public static void setArgentbis(String id, float argent){
		for (int i = 0; i < artistes.size(); i++){
			if(((Artiste)artistes.objs[i]).getId() == id){
				((Artiste)artistes.objs[i]).setArgent(argent);
			}
		}
	}
	

	// To schedule an agent on the fly
	public static void scheduleAgent(Steppable agent){
		instance.schedule.scheduleRepeating(agent);
	}
	

}
